package com.core.service;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.core.dao.DAOException;
import com.core.dao.DAOFactory;
import com.core.dao.impl.ContactDAOImpl;
import com.core.entities.Catagory;
import com.core.entities.Contact;
import com.core.entities.OnlineAccount;
import com.core.entities.Work;
import com.core.gui.helper.UserInfo;

public class ContactSrv {
	
	private Connection conn;
	private ContactDAOImpl contactDAOImpl;
	
	public ContactSrv(Connection conn) {
		this.conn = conn;
	}
	
	public void getContactsNames(){
		List<String> contactsNames = new ArrayList<String>();
		try{
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).selectAll())
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
	}
	public void getContactsNamesByCity(String cityName){
		List<String> contactsNames = new ArrayList<String>();
		try{
			int cityId = DAOFactory.getInstance().getCityDAO(conn).getByName(cityName).get(0).getId();
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByCityId(cityId))
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
	}
	
	// do this method by list to know if we have any duplicate username.
	
	public List<String> getContactNameByUsername(String username){
		List<String> contactsNames = new ArrayList<String>();
		try{
			int usernameId = DAOFactory.getInstance().getUsernameDAO(conn).getByUsername(username).get(0).getId();
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByUsernameId(usernameId))
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}
	
	public List<String> getContactsByWork(String workName){
		List<String> contactsNames = new ArrayList<String>();
		try{
			int workId = DAOFactory.getInstance().getWorkDAO(conn).getByCompnyName(workName).get(0).getId();
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByWorkId(workId))
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;

	}
	
	public List<String> getContactsByTwitterOnlineAccount(String twitterOnlineAccountName){
		List<String> contactsNames = new ArrayList<String>();
		try{
			int onlineAccountId = DAOFactory.getInstance().getOnlineAccountDAO(conn)
					.getByTwitter(twitterOnlineAccountName).get(0).getId();
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByOnlineAccountId(onlineAccountId))
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;

	}
	public List<String> getContactsByGoogleOnlineAccount(String googleOnlineAccountName){
		List<String> contactsNames = new ArrayList<String>();
		try{
			int onlineAccountId = DAOFactory.getInstance().getOnlineAccountDAO(conn)
					.getByGoogle(googleOnlineAccountName).get(0).getId();
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByOnlineAccountId(onlineAccountId))
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;

	}
	public List<String> getContactsByFacebookOnlineAccount(String facebookOnlineAccountName){
		List<String> contactsNames = new ArrayList<String>();
		try{
			int onlineAccountId = DAOFactory.getInstance().getOnlineAccountDAO(conn)
					.getByFacebook(facebookOnlineAccountName).get(0).getId();
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByOnlineAccountId(onlineAccountId))
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;

	}
	public List<String> getContactsByCatagory(String catagoryName){
		List<String> contactsNames = new ArrayList<String>();
		try{
			int catagoryId = DAOFactory.getInstance().getCatagoryDAO(conn)
					.getByName(catagoryName).get(0).getId();
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByCatagoryId(catagoryId))
				contactsNames.add(contact.getFirstName() + contact.getSecondName() + contact.getThirdName()
						+contact.getLastName());
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;

	}
	
	public int getContactPK(String contactName){
		try{
			return DAOFactory.getInstance().getContactDAO(conn).getByFirstName(contactName).get(0).getId();
		}catch(DAOException e){
			e.printStackTrace();
		}
		return -1;
	}
	
	
	public ContactDAOImpl getContactDAO() throws DAOException{
		if(contactDAOImpl == null)
			contactDAOImpl = (ContactDAOImpl) DAOFactory.getInstance().getContactDAO(conn);
		return contactDAOImpl;
	}
	
	public void registe(OnlineAccount workOnlineAccount, Work work, 
			OnlineAccount contactOnlineAccount, Contact contact) throws DAOException {	
	
		
		///***************contact online account ************///
		
    	contactOnlineAccount.setFacebook(contactOnlineAccount.getFacebook());
		contactOnlineAccount.setTwitter(contactOnlineAccount.getTwitter());
		contactOnlineAccount.setGoogle(contactOnlineAccount.getGoogle());
		DAOFactory.getInstance().getOnlineAccountDAO().insert(contactOnlineAccount);// insert contact online account

		///*********** work online account ************///
		
		workOnlineAccount.setFacebook(workOnlineAccount.getFacebook());
		workOnlineAccount.setTwitter(workOnlineAccount.getTwitter());
		workOnlineAccount.setGoogle(workOnlineAccount.getGoogle());
		DAOFactory.getInstance().getOnlineAccountDAO().insert(workOnlineAccount); // insert work online account

		///*********** work************///
		
		work.setCompnyName(work.getCompnyName());
		work.setLandPhone(work.getLandPhone());
		work.setMobilePhone(work.getMobilePhone());
		work.setEmail(work.getEmail());
		work.setWebsite(work.getWebsite());
		work.setCityId(work.getCityId());

		int workOnlineAccountId = UserInfo.getInstance().getService()
				.getOnlineAccountSrv()
				.getFacebookOnlineAccountPk(workOnlineAccount.getFacebook());
		
		work.setOnlineAccountId(workOnlineAccountId);
		DAOFactory.getInstance().getWorkDAO().insert(work); // insert work



		///********* contact ********///
		
		contact.setFirstName(contact.getFirstName());
		contact.setSecondName(contact.getSecondName());
		contact.setThirdName(contact.getThirdName());
		contact.setLastName(contact.getLastName());
		contact.setAddress(contact.getAddress());
		contact.setCityId(contact.getCityId());
		contact.setMobileNumber(contact.getMobileNumber());
		contact.setLandPhone(contact.getLandPhone());
		contact.setEmail(contact.getEmail());
		contact.setWebsite(contact.getWebsite());
		
		int contactOnlineAccountId = UserInfo.getInstance().getService()
				.getOnlineAccountSrv()
				.getFacebookOnlineAccountPk(contactOnlineAccount.getFacebook());

		contact.setOnlineAccountId(contactOnlineAccountId);

		contact.setJob(contact.getJob());

		int workId = UserInfo.getInstance().getService().getWorkSrv()
				.getWorkPK(work.getCompnyName());

		contact.setWorkId(workId);		
		contact.setCatagoryId(contact.getCatagoryId());

		DAOFactory.getInstance().getContactDAO().insert(contact);

	}
	
	
	// search methods
	public List<Contact> getContactByFirstName(String firstName){
		List<Contact> contactsNames = new ArrayList<Contact>();
		try{
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByFirstName(firstName))
				contactsNames.add(contact);
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}
	
	public List<Contact> getContactByLastName(String lastName){
		List<Contact> contactsNames = new ArrayList<Contact>();
		try{
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByLastName(lastName))
				contactsNames.add(contact);
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}
	
	public List<Contact> getContactByCityId(String cityName){
		List<Contact> contactsNames = new ArrayList<Contact>();
		try{
			int cityId = UserInfo.getInstance().getService().getCitySrv().getCityPK(cityName);
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByCityId(cityId))
				contactsNames.add(contact);
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}
	public List<Contact> getContactByCatagoryId(String catagoryName){
		List<Contact> contactsNames = new ArrayList<Contact>();
		try{
			int catagoryId = UserInfo.getInstance().getService().getCatagorySrv().getCatagoryPK(catagoryName);
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByCatagoryId(catagoryId))
				contactsNames.add(contact);
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}
	
	public List<Contact> getContactByOnlineAccount(String facebook){
		List<Contact> contactsNames = new ArrayList<Contact>();
		try{
			int onlineAccountId = UserInfo.getInstance().getService().getOnlineAccountSrv().getFacebookOnlineAccountPk(facebook);
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByOnlineAccountId(onlineAccountId))
				contactsNames.add(contact);
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}
	
	public List<Contact> getContactByEmail(String email){
		List<Contact> contactsNames = new ArrayList<Contact>();
		try{
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByEmail(email))
				contactsNames.add(contact);
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}
	
	public List<Contact> getContactByMobilePhone(String mobilePhone){
		List<Contact> contactsNames = new ArrayList<Contact>();
		try{
			for(Contact contact : DAOFactory.getInstance().getContactDAO(conn).getByMobileNumber(mobilePhone))
				contactsNames.add(contact);
		}catch(DAOException e){
			e.printStackTrace();
		}
		return contactsNames;
	}	
	
//	public List<Contact> searchContact(Contact contact){
//		
//		List<Contact> contactsNames = new ArrayList<Contact>();
//		
//		getContactByCatagoryId(contact.getCatagory().getName());
//		getContactByCityId(contact.getCity().getName());
//		getContactByFirstName(contact.getFirstName());
//		getContactByLastName(contact.getLastName());
//		getContactByMobilePhone(contact.getMobileNumber());
//		getContactByEmail(contact.getEmail());
//		getContactByOnlineAccount(contact.getOnlineAccount().getFacebook());
//		
//		contactsNames.add(contact);
//		
//		return contactsNames;
//	}

	String condition;
	Boolean firstParam;
	public List<Contact> searchContacts(Contact contact, boolean useLike){
		condition = " WHERE ";
		firstParam = true;
		List params = new ArrayList();
		List<Contact> result = new ArrayList<Contact>();
		
		if(contact.getFirstName() != null && !contact.getFirstName().equals("")){
			condition = checkFirstParam();
			condition +="first_name=?";
			params.add(contact.getFirstName());
		}
		if(contact.getSecondName() != null && !contact.getSecondName().equals("")){
			condition = checkFirstParam();
			condition +="second_name=?";
			params.add(contact.getSecondName());
		}
		if(contact.getThirdName() != null && !contact.getThirdName().equals("")){
			condition = checkFirstParam();
			condition +="third_name=?";
			params.add(contact.getThirdName());
		}
		if(contact.getLastName() != null && !contact.getLastName().equals("")){
			condition = checkFirstParam();
			condition +="Last_name=?";
			params.add(contact.getLastName());
		}
		if(contact.getAddress() != null && !contact.getAddress().equals("")){
			condition = checkFirstParam();
			condition +="address=?";
			params.add(contact.getAddress());
		}
		if(contact.getCity().getName() != null && !contact.getCity().getName().equals("")){
			condition = checkFirstParam();
			condition +="city_id=?";
			params.add(contact.getCity().getName());
		}
		if(contact.getLandPhone() != null && !contact.getLandPhone().equals("")){
			condition = checkFirstParam();
			condition +="land_phone=?";
			params.add(contact.getLandPhone());
		}
		if(contact.getMobileNumber() != null && !contact.getMobileNumber().equals("")){
			condition = checkFirstParam();
			condition +="mobile_number=?";
			params.add(contact.getMobileNumber());
		}
		if(contact.getEmail() != null && !contact.getEmail().equals("")){
			condition = checkFirstParam();
			condition +="email=?";
			params.add(contact.getEmail());
		}
		if(contact.getWorkId() != null && !contact.getWorkId().equals("")){
			condition = checkFirstParam();
			condition +="work_id=?";
			params.add(contact.getWorkId());
		}
		if(contact.getOnlineAccountId() != null && !contact.getOnlineAccountId().equals("")){
			condition = checkFirstParam();
			condition +="online_account_id=?";
			params.add(contact.getOnlineAccountId());
		}
		if(contact.getCatagoryId() != null && !contact.getCatagoryId().equals("")){
			condition = checkFirstParam();
			condition +="catagory_id=?";
			params.add(contact.getCatagoryId());
		}
		try{
			result = DAOFactory.getInstance().getContactDAO(conn).select(condition, params.toArray());
		}catch (DAOException e){
			e.printStackTrace();
		}
		return result;
		
	}
	
	private String checkFirstParam(){
		if(!firstParam){
			condition +=" AND ";
		}else
			firstParam = false;
		return condition;
	}
}
